package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.vo.AttrGroupDetailEntity;
import com.atguigu.gmall.pms.service.AttrService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<AttrGroupEntity> getAttributeGroup(Long id) {
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrGroupEntity::getCategoryId, id);
        return this.list(wrapper);
    }


    @Override
    public List<AttrGroupDetailEntity> getGroupDetail(Long categoryId) {


        //获取List<GroupEntity>
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrGroupEntity::getCategoryId, categoryId);
        List<AttrGroupEntity> groups = this.list(wrapper);

        /*//将GroupEntities封装成GroupDetails
        List<AttrGroupDetailEntity> groupDetails = new ArrayList<>();
        for (AttrGroupEntity group : groups) {
            //在每个groupEntity里面查询到对应的list<attr>
            LambdaQueryWrapper<AttrEntity> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(AttrEntity::getGroupId,group.getId()).eq(AttrEntity::getType,1);//同时，Type=1的属性我们才要
            List<AttrEntity> attrs = attrService.list(wrapper2);
            //封装成GroupDetail
            AttrGroupDetailEntity groupDetail = new AttrGroupDetailEntity();
            BeanUtils.copyProperties(group,groupDetail);
            groupDetail.setAttrEntities(attrs);
            //将封装好的GroupDetail放入groupDetails集合里面
            groupDetails.add(groupDetail);
        }*/
        //以上代码可以通过steamAPI进行优化,原来要new一个新的list用来接收封装结果，现在一步到位，少写一个new ArrayList
        List<AttrGroupDetailEntity> groupDetails = groups.stream().map(group -> {

            //封装成GroupDetail
            AttrGroupDetailEntity groupDetail = new AttrGroupDetailEntity();
            BeanUtils.copyProperties(group, groupDetail);

            //在每个groupEntity里面查询到对应的list<attr>
            groupDetail.setAttrEntities(attrService
                    .list( new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getGroupId, group.getId()).eq(AttrEntity::getType, 1)));//同时，Type=1的属性我们才要

            return groupDetail;
        }).collect(Collectors.toList());

        return groupDetails;
    }

}